package com.googlecode.autopp.driver;

import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Date;
import java.util.Random;

/**
 * Driver which generates random data.
 */
public class RandomDriver extends SimpleGenericDriver {

    private static Random random = new Random();

    @Override
    public boolean getBoolean() {
        return random.nextFloat() > 0.5 ? true : false;
    }

    @Override
    public Color getColor() {
        return Color.getHSBColor(random.nextFloat(), random.nextFloat(), random.nextFloat());
    }

    @Override
    public Date getDate() {
        return new Date(random.nextLong());
    }

    @Override
    public double getDouble() {
        return random.nextDouble();
    }

    @Override
    public float getFloat() {
        return random.nextFloat();
    }

    @Override
    public int getInteger() {
        return random.nextInt();
    }

    @Override
    public long getLong() {
        return random.nextLong();
    }
    
    @Override
    public byte getByte() {
        return (byte)random.nextInt();
    }

    @Override
    public String getString() {
        String letters = "abcdefgijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i++) {
            sb.append(letters.charAt(Math.abs(getInteger()) % letters.length()));
        }
        return sb.toString();
    }

    @Override
    public URI getURI() {
        try {
            return new URI(getString());
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException("Cannot create URI object.", e);
        }
    }

    @Override
    public URL getURL() {
        try {
            return new URL("http://" + getString());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Cannot create URL object.", e);
        }
    }

    @Override
    public Enum<?> getEnum(Class<Enum<?>> enumClass) {
        Enum<?>[] tab = enumClass.getEnumConstants();
        return tab.length > 0 ? tab[Math.abs(random.nextInt()) % tab.length] : null;
    }

}
